package com.dries.portal.module.circle.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.circle.entity.Circle;
import com.dries.portal.module.circle.entity.CircleLikes;
import com.dries.portal.module.circle.from.AddCircleFrom;
import com.dries.portal.module.circle.service.ICircleLikesService;
import com.dries.portal.module.circle.service.ICircleService;
import com.dries.portal.module.circle.vo.CircleDetailVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 圈子动态
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-26 14:27:42
 */
@RestController
@Api(tags = "用户动态API", value = "CircleController")
@RequestMapping("circle")
public class CircleController {

    @Resource
    private ICircleService circleService;
    @Resource
    private ICircleLikesService circleLikesService;

    /**
     * 列表
     */
    @ApiOperation("动态列表")
    @GetMapping("/list/{userId}")
    public CommonResult list(@PathVariable String userId, @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                             @RequestParam(defaultValue = "1", required = false) Integer pageNum){

        // 1.开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 2.查询数据
        List<CircleDetailVo> circles = circleService.getCircleList(pageNum, pageSize, userId, true);
        if (CollUtil.isNotEmpty(circles)) {
            // 3.装载数据
            return CommonResult.success(new PageInfo<>(circles));
        } else {
            // 3.装载数据
            return CommonResult.success("暂无数据");
        }

    }

    /**
     * 列表
     */
    @ApiOperation("我的动态列表")
    @GetMapping("mine/{userId}")
    public CommonResult mine(@PathVariable String userId, @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                             @RequestParam(defaultValue = "1", required = false) Integer pageNum){

        // 1.开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 2.查询数据
        List<CircleDetailVo> circles = circleService.getCircleList(pageNum, pageSize, userId, false);
        if (CollUtil.isNotEmpty(circles)) {
            // 3.装载数据
            return CommonResult.success(new PageInfo<>(circles));
        } else {
            // 3.装载数据
            return CommonResult.success("暂无数据");
        }

    }


    /**
     * 信息
     */
    @ApiOperation("动态详情")
    @GetMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Integer id){

		CircleDetailVo detailVo = circleService.getCircleDetailById(id);

        return CommonResult.success(detailVo);
    }

    /**
     * 保存
     */
    @ApiOperation("发动态")
    @PostMapping("/save")
    public CommonResult save(@RequestBody AddCircleFrom circle){

        return circleService.saveAddCircleFrom(circle);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Circle circle){
		circleService.updateById(circle);

        return CommonResult.success();
    }

    /**
     * 删除动态
     */
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] ids){
		circleService.removeByIds(Arrays.asList(ids));
        return CommonResult.success();
    }

    /**
     * 点赞数量
     */
    @ApiOperation("点赞数")
    @GetMapping("/likes/count/{id}")
    public CommonResult count(@PathVariable Integer id){

        int count = circleLikesService.count(new QueryWrapper<CircleLikes>().lambda()
                .eq(CircleLikes::getCircleId, id)
        );

        return CommonResult.success(count);

    }

}
